import redis
import json
import hashlib


class RedisUtil(object):
    def __init__(self):
        self.chat_room_nick_set = 'chat_room_nick_set'
        self.cookie_nick = 'cookie-{}'
        self.chat_list ='chat_room_list'

        # 你需要在这里初始化Redis
        self.client = redis.Redis()


    def is_nick_already_exists(self, nick):
        """
        """
        return not self.client.sadd(self.chat_room_nick_set,nick)

    def set_token(self, nick, token):
        """
        """
        key = self.cookie_nick.format(nick)
        return self.client.set(key, token)

    def get_token(self, nick):
    
        key = self.cookie_nick.format(nick)
        return self.client.get(key).decode()


    def get_chat_list(self):
        
        msgs = self.client.lrange(self.chat_list,0,20)
        return [json.loads(s) for s in msgs]

    def get_nick_msg_expire_time(self, nick, msg):
        """
        """
        msg_md5 = hashlib.md5(msg.encode()).hexdigest()
        return 0

    def push_chat_info(self, chat_info):
        """
        """
        info = json.dumps(chat_info)
        self.client.lpush(self.chat_list, info)
        self.client.ltrim(self.chat_list, 0, 20)

        return None

    def set_nick_msg_expire_time(self, nick, msg):
        """
        """
        msg_md5 = hashlib.md5(msg.encode()).hexdigest()
       
        return None
